﻿using FairyGUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public class HeroPage : BasePage
{
    private GList lsFightHero;
    private GList lsEquip;
    private GButton btnHeroInfo;
    private GButton btnHeroInfo1;
    private GButton btnZhaoMu;
    private GButton btnShangZhen;
    private GButton btnJob;
    private GButton btnChangeHero;
    private Transition transZhaoMu;

    private string[] defaultEuqipName;
    private Color[] defaultEquipColor;

    public HeroPage(GComponent vie) : base(vie)
    {
        lsFightHero = view.GetChild("n0").asCom.GetChild("n0").asList;
        lsFightHero.onClickItem.Add(OnHeroListItemClick);
        lsEquip = view.GetChild("n35").asCom.GetChild("n0").asList;
        btnJob = view.GetChild("n7").asButton;
        btnChangeHero = view.GetChild("n49").asButton;
        btnChangeHero.title = "换";
        btnChangeHero.onClick.Add(OnChangeHeroClick);

        btnHeroInfo = view.GetChild("n5").asButton;
        btnHeroInfo.onClick.Add(OnRoleInfoClick);
        btnZhaoMu = view.GetChild("n28").asButton;
        transZhaoMu = btnZhaoMu.GetTransition("t0");

        btnZhaoMu.onClick.Add(OnZhaoMu);

        btnHeroInfo1 = view.GetChild("n6").asButton;
        btnHeroInfo1.onClick.Add(OnRoleInfo1Click);

        btnShangZhen = UIPackage.CreateObject("Main", "btn_shangzhen").asButton;

        InitEquipInfo();
    }

    public override void Show()
    {
        MsgListenerMgr.AddMsgListener(MsgListenerType.ShangZhen, OnShangZhenMsg);
        MsgListenerMgr.AddMsgListener(MsgListenerType.EquipPosChange, OnEquipPosChangeMsg);
        MsgListenerMgr.AddMsgListener(MsgListenerType.ZhaoMuOk, OnZhaoMuOk);
        MsgListenerMgr.AddMsgListener(MsgListenerType.ZhaoMuTimeZero, OnZhaoMuTimeZero);
        Load();

        if (GameNetwork.Player.BaseInfo.ZhaoMuRemainTime <= 0)
        {
            transZhaoMu.Play(-1,0,null);
        }
    }

    public override void Close()
    {
        MsgListenerMgr.RemoveMsgListener(MsgListenerType.ShangZhen, OnShangZhenMsg);
        MsgListenerMgr.RemoveMsgListener(MsgListenerType.EquipPosChange, OnEquipPosChangeMsg);
        MsgListenerMgr.RemoveMsgListener(MsgListenerType.ZhaoMuOk, OnZhaoMuOk);
        MsgListenerMgr.RemoveMsgListener(MsgListenerType.ZhaoMuTimeZero, OnZhaoMuTimeZero);
    }

    private void OnShangZhenMsg(params object[] para)
    {
        long newId = (long)para[0];

        if (GameNetwork.Player.FightHeroMgr.TryGetById(newId, out Hero hero))
        {
            AddOrUpdateHero(hero);
            if (lsFightHero.numChildren >= 7)
            {
                lsFightHero.RemoveChild(btnShangZhen);
            }

            lsFightHero.selectedIndex = hero.szPos - 1;
            lsFightHero.onClickItem.Call();
        }
    }

    private void OnEquipPosChangeMsg(params object[] para)
    {
        byte szPos = (byte)para[0];
        if (szPos == 0 || szPos > lsFightHero.numChildren)
        {
            return;
        }

        GButton button = lsFightHero.GetChildAt(szPos - 1).asButton;
        if (button.data == null || (byte)button.data != szPos)
        {
            return;
        }

        if (button.selected)
        {
            UpdateEquip(szPos);

            if (GameNetwork.Player.FightHeroMgr.TryGetByPos(szPos, out Hero hero))
            {
                UpdateRoleAttribute(hero);
            }
        }
    }

    private void Load()
    {
        if (lsFightHero.numChildren != 0)
        {
            return;
        }

        List<Hero> heros = GameNetwork.Player.FightHeroMgr.GetAll();
        for (int i = 0; i < heros.Count; i++)
        {
            AddOrUpdateHero(heros[i]);
        }

        if (lsFightHero.numChildren < 6)
        {
            lsFightHero.AddChild(btnShangZhen);
            btnShangZhen.onClick.Set(OnShangZhenClick);
        }
        lsFightHero.selectedIndex = 0;
        lsFightHero.onClickItem.Call();
    }

    private void AddOrUpdateHero(Hero hero)
    {
        InitHero initRole = hero.InitHero;
        if (initRole == null)
        {
            return;
        }

        int idx = hero.szPos - 1;
        GButton btn = idx < lsFightHero.numChildren-1 ? lsFightHero.GetChildAt(idx).asButton : null;
        if (btn == null)
        {
            btn = UIPackage.CreateObject("Main", "btn_head").asButton;
            lsFightHero.AddChildAt(btn, idx);
        }

        btn.data = hero.szPos;

        GTextField tfName = btn.GetChild("n2").asTextField;
        string name = hero.sId == 1 ? GameNetwork.Player.Name : initRole.name;
        tfName.text = string.Format("{0}+{1}", name, hero.JinJieLevel);
        tfName.color = CommonUtil.GetHeroPinZhiColor(initRole.quality);

        GTextField tfDuJie = btn.GetChild("n3").asTextField;
        tfDuJie.text = CommonUtil.GetDuJieName(hero.DuJieLevel);
        tfDuJie.color = CommonUtil.GetHeroDuJieColor(hero.DuJieLevel);
    }

    private void OnHeroListItemClick(EventContext context)
    {
        Hero selectHero = SelectHero;
        if (selectHero == null)
        {
            return;
        }

        UpdateRoleAttribute(selectHero);
        UpdateEquip(selectHero.szPos);
    }

    private void UpdateRoleAttribute(Hero hero)
    {
        btnChangeHero.visible = !hero.IsPlayer();

        if (hero.InitHero != null)
        {
            btnJob.title = CommonUtil.GetJobName(hero.InitHero.job - 1);
        }

        Attribute attribute = GameNetwork.Player.FighAttributeMgr.Get(hero.szPos);

        GTextField tfATK = view.GetChild("n13").asTextField;
        tfATK.text = CommonUtil.NumberToString(attribute.Get(AttributeType.ATK));

        GTextField tfHP = view.GetChild("n14").asTextField;
        tfHP.text = CommonUtil.NumberToString(attribute.Get(AttributeType.HP));

        GTextField tfDEF = view.GetChild("n15").asTextField;
        tfDEF.text = CommonUtil.NumberToString(attribute.Get(AttributeType.DEF));

        GTextField tfRGS = view.GetChild("n16").asTextField;
        tfRGS.text = CommonUtil.NumberToString(attribute.Get(AttributeType.RGS));
    }

    private void UpdateEquip(byte szPos)
    {
        List<Equip> equips = GameNetwork.Player.FightEquipMgr.GetAll(szPos);
        for (int i=0; i<lsEquip.numChildren; i++)
        {
            GButton button = lsEquip.GetChildAt(i).asButton;

            bool isFind = false;
            foreach (Equip equip in equips)
            {
                if (equip.InitEquip != null && equip.InitEquip.kind == i+1)
                {
                    button.title = equip.InitEquip.name;
                    button.titleColor = CommonUtil.GetEquipColor(equip.InitEquip.quality);
                    isFind = true;
                    break;
                }
            }
            if (!isFind)
            {
                button.title = defaultEuqipName[i];
                button.titleColor = defaultEquipColor[i];
            }
        }
    }

    private void OnRoleInfoClick(EventContext context)
    {
        Hero hero = SelectHero;
        if (hero == null)
        {
            return;
        }

        PopupPanelManager.Open<HeroInfoPanel>(hero);
    }

    private void OnRoleInfo1Click(EventContext context)
    {
        Hero hero = SelectHero;
        if (hero == null)
        {
            return;
        }

        PopupPanelManager.Open<HeroInfo1Panel>(hero.szPos);
    }

    private void InitEquipInfo()
    {
        GObject[] gObjects = lsEquip.GetChildren();
        defaultEuqipName = new string[gObjects.Length];
        defaultEquipColor = new Color[gObjects.Length];
        for (int i = 0; i < gObjects.Length; i++)
        {
            GButton btn1 = gObjects[i].asButton;
            btn1.data = i+1;
            btn1.onClick.Add(OnEquipClick);
            defaultEuqipName[i] = btn1.title;
            defaultEquipColor[i] = btn1.titleColor;
        }
    }

    private void OnEquipClick(EventContext context)
    {
        GButton btn1 = context.sender as GButton;
        int equipType = (int)btn1.data;

        Hero hero = SelectHero;
        if (hero == null)
        {
            return;
        }

        if (!GameNetwork.Player.FightEquipMgr.TryGetByType(hero.szPos, equipType, out Equip equip))
        {
            CommonUtil.ChangeEquip(hero.szPos, equipType);
        }
        else
        {
            PopupPanelManager.Open<EquipInfoPanel>(equip);
        }
    }

    public Hero SelectHero 
    { 
        get
        {
            if (lsFightHero.selectedIndex < 0)
            {
                return null;
            }

            GButton btnHead = lsFightHero.GetChildAt(lsFightHero.selectedIndex).asButton;
            if (btnHead == null || btnHead.data == null)
            {
                return null;
            }

            byte szPos = (byte)btnHead.data;
            if (GameNetwork.Player.FightHeroMgr.TryGetByPos(szPos, out Hero hero))
            {
                return hero;
            }

            return null;
        } 
    }

    private void OnShangZhenClick(EventContext context)
    {
        CommonUtil.ChangeHero(0);
        lsFightHero.ScrollToView(lsFightHero.numChildren-1);
    }

    private void OnChangeHeroClick(EventContext context)
    {
        if (lsFightHero.selectedIndex < 0)
        {
            return;
        }

        CommonUtil.ChangeHero((byte)(lsFightHero.selectedIndex+1));
    }

    private void OnZhaoMu(EventContext context)
    {
        PopupPanelManager.Open<TavernPanel>();
    }

    private void OnZhaoMuOk(object[] para)
    {
        transZhaoMu.Stop();
    }

    private void OnZhaoMuTimeZero(object[] para)
    {
        transZhaoMu.Play(-1, 0, null);
    }
}
